from django.db import models
from django.contrib.auth.models import User  # 导入 Django 内置用户模型


class BaseModel(models.Model):
    created_at = models.DateTimeField(
        auto_now_add=True, verbose_name="创建时间", null=True, blank=True, db_comment='创建时间')
    updated_at = models.DateTimeField(
        auto_now=True, verbose_name="更新时间", null=True, blank=True, db_comment='更新时间')
    active = models.BooleanField(
        default=True, verbose_name="激活", null=True, blank=True, db_comment='是否激活')
    created_by = models.ForeignKey(
        User,  # 关联到 Django 内置 User 模型
        null=True,
        blank=True,
        verbose_name='创建人',
        db_comment='创建人',
        on_delete=models.SET_NULL,
        related_name='%(class)s_created'  # 动态生成反向关系名
    )
    updated_by = models.ForeignKey(
        User,
        null=True,
        blank=True,
        verbose_name='更新人',
        db_comment='更新人',
        on_delete=models.SET_NULL,
        related_name='%(class)s_updated'
    )

    class Meta:
        abstract = True


class UserExtraction(BaseModel):
    user_id = models.BigIntegerField(
        verbose_name='用户ID', null=True, blank=True, db_comment='用户ID')
    avator = models.CharField(
        max_length=255, verbose_name='头像', null=True, blank=True, db_comment='用户头像')
    level = models.IntegerField(
        default=0, verbose_name='等级', db_comment='用户等级')
    phone_number = models.CharField(
        max_length=20, verbose_name='手机号', null=True, blank=True, db_comment='用户手机号')

    def __str__(self):
        return self.phone_number or ''

    class Meta:
        db_table = 'app_user_extraction'
        verbose_name = '用户额外信息'
        verbose_name_plural = '01.用户管理'
        db_table_comment = '用户额外信息表'


class Menu(BaseModel):
    name = models.CharField(
        max_length=255, verbose_name='菜单名称', db_comment='菜单名称')
    path = models.CharField(
        max_length=255, verbose_name='菜单路径', db_comment='菜单路径')
    icon = models.CharField(
        max_length=36, verbose_name='菜单图标', db_comment='菜单图标')
    order = models.IntegerField(default=0, verbose_name='排序', db_comment='排序')
    tag = models.CharField(
        max_length=36, verbose_name='菜单标签', db_comment='菜单标签')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'app_menu'
        verbose_name = '菜单'
        verbose_name_plural = '02.菜单管理'
        db_table_comment = '菜单表'
